package com.chris.homecare.components
{
	import com.chris.homecare.TextureAtlasManager;
	import com.chris.homecare.data.EventData;
	import com.chris.homecare.utils.CommonUtils;
	
	import flash.geom.Rectangle;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.LayoutGroup;
	import feathers.controls.ScrollText;
	import feathers.display.Scale9Image;
	import feathers.layout.AnchorLayout;
	import feathers.layout.AnchorLayoutData;
	import feathers.textures.Scale9Textures;
	
	import starling.events.Event;
	
	public class EventPopupComonent extends LayoutGroup
	{
		private var backgroundImage:Scale9Image;
		private var eventData:EventData;

		private var organiserGroup:NameValuePairLabel;

		private var dateGroup:NameValuePairLabel;

		private var locationGroup:NameValuePairLabel;

		private var headingLabel:Label;
		private var bulletHeadingComp:BulletHeadingComp;

		private var scrollText:ScrollText;

		private var okButton:Button;
		
		public function EventPopupComonent(eventData:EventData)
		{
			this.eventData = eventData;
		}
		
		override protected function initialize():void
		{
			this.layout = new AnchorLayout();
			
			
			const texture:Scale9Textures = new Scale9Textures(TextureAtlasManager.getAtlas().getTexture("popupBackground"), new Rectangle(20,20,60,60));
			backgroundImage = new Scale9Image(texture);
			addChild(backgroundImage);
			
			/*headingLabel = new Label();
			headingLabel.text = eventData.titel;
			headingLabel.nameList.add("updates-heading-lbl");
			addChild(headingLabel);*/
			
			bulletHeadingComp = new BulletHeadingComp(eventData.titel);
			bulletHeadingComp.y = 20*CommonUtils.scaleValue;
			addChild(bulletHeadingComp);
			
			/*var headingLabelLayoutData:AnchorLayoutData = new AnchorLayoutData();
			headingLabelLayoutData.horizontalCenter = 0;
			headingLabelLayoutData.top = 20;
			headingLabel.layoutData = headingLabelLayoutData;*/
			
			organiserGroup = new NameValuePairLabel("Time", eventData.organiser);
			dateGroup = new NameValuePairLabel("Date", eventData.date.toDateString());
			locationGroup = new NameValuePairLabel("Location", eventData.location);
			
			organiserGroup.x = dateGroup.x = locationGroup.x = 15;
			
			addChild(organiserGroup);
			addChild(dateGroup);
			addChild(locationGroup);
			
			okButton = new Button();
			okButton.addEventListener(Event.TRIGGERED, onOkTriggered);
			okButton.nameList.add("evt-ok-btn");
			okButton.label = "OK";
			this.addChild(okButton);
			
			var okButtonLayoutData:AnchorLayoutData = new AnchorLayoutData();
			okButtonLayoutData.bottom = 25;
			okButtonLayoutData.horizontalCenter = 0;
			okButton.layoutData = okButtonLayoutData;
			
			scrollText = new ScrollText();
			scrollText.nameList.add("custom-evt-scroll-txt");
			scrollText.text = eventData.details;
			this.addChild(scrollText);
			
			super.initialize();
		}
		
		override protected function draw():void
		{
			this.width = backgroundImage.width = stage.stageWidth - 40;
			this.height = backgroundImage.height = stage.stageHeight - 40;
			
			organiserGroup.y = bulletHeadingComp.height + 30;
			dateGroup.y = organiserGroup.y + organiserGroup.height + 5;
			locationGroup.y = dateGroup.y + dateGroup.height + 5;
			
			scrollText.x = 10;
			scrollText.width = stage.stageWidth - 60;
			scrollText.y = locationGroup.y + locationGroup.height + 15;
			var availHeight:Number = stage.stageHeight - (locationGroup.y + locationGroup.height + okButton.height + 100);
			scrollText.height = availHeight;
			
			super.draw();
		}
		
		private function onOkTriggered(event:Event):void
		{
			this.dispatchEventWith(Event.COMPLETE, true);
		}
	}
}